{template 'common/header'}
<script type="text/javascript">
<!--
	var packet = {php echo $packet ? json_encode($packet) : 'null';};
	angular.module('cloudApp');
	angular.module('cloudApp')
	.controller('FileProcessorCtrl', function($scope, $http, config) {
		$scope.files = config.files;
		$scope.fails = [];

		var total = $scope.files.length;
		var i = 1;
		var errormsg = '';
		var tasknum = config.tasknum && config.type != '' ? config.tasknum : 1;
		if (tasknum > total) {
			tasknum = 1;
		}
		$scope.proc = function() {
			var path = $scope.files.pop();
			if(!path && i >= total) {
				if ($scope.fails && $scope.fails.length > 0) {
					util.message('保证写入权限，如有文件写入失败，可尝试F5刷新重复更新。', config.appname ? './index.php?c=module&a=manage-system&do=installed' : './index.php?c=cloud&a=upgrade&', 'info');
				} else {
					var targetUrl = '';
					if (config.type == 'theme') {
						targetUrl = './index.php?c=cloud&a=process&step=schemas&t=' + config.appname + '&is_upgrade=' + config.is_upgrade;
					} else if (config.type == 'webtheme') {
						targetUrl = './index.php?c=cloud&a=process&step=schemas&w=' + config.appname + '&is_upgrade=' + config.is_upgrade;
					} else {
						targetUrl = './index.php?c=cloud&a=process&step=schemas&module_name=' + config.appname + '&is_upgrade=' + config.is_upgrade + '&batch=1&support=' + config.account_type + '&application_type=' + config.application_type +  '&has_new_support={$has_new_support}';
					}
					util.message('文件同步完成，正在处理数据同步......', targetUrl, 'success');
				}
				return;
			}
			$scope.file = path;
			$scope.pragress = i + '/' + total;
			$scope.pragressPer = Math.floor((i / total) * 100);
			$scope.topPragress = $scope.pragressPer < 50 ? $scope.pragressPer * 3.6 - 135 : 45;
			$scope.bottomPragress = $scope.pragressPer > 50 ? ($scope.pragressPer - 50) * 3.6 - 135 : -135;
			var params = {path: path, type : config.type};
			$http.post(location.href, params).success(function(dat){
				i++;
				if(dat != 'success') {
					$scope.fails.push('['+dat+'] ' + path);
					errormsg = dat;
				}
				$scope.proc();
			}).error(function(){
				i++;
				$scope.fails.push(path);
				$scope.proc();
			});
		}
		for (j = 0; j < tasknum; j++) {
			$scope.proc();
		}
	})
	.controller('SchemasProcessorCtrl', function($scope, $http, config){
		if (packet && (packet.files.length > 0)) {
			util.message('请先完成文件同步', './index.php?c=cloud&a=upgrade');
			return false;
		}
		$scope.schemas = config.schemas;
		$scope.fails = [];
		var is_m_install = config.is_module_install;
		var total = $scope.schemas.length;
		var i = 1;
		var error = function() {
			util.message('未能成功执行处理数据库, 请联系开发商解决. ');
		}
		var proc = function() {
			var schema = $scope.schemas.pop();
			if(!schema) {
				if($scope.fails.length > 0) {
					error();
					return;
				} else {
					if(is_m_install == 1) {
						location.href = '';
					} else {
						location.href = '';
					}
					return;
				}
			}
			$scope.schema = schema;
			$scope.pragress = i + '/' + total;
			$scope.pragressPer = Math.floor((i / total) * 100);
			$scope.topPragress = $scope.pragressPer < 50 ? $scope.pragressPer * 3.6 - 135 : 45;
			$scope.bottomPragress = $scope.pragressPer > 50 ? ($scope.pragressPer - 50) * 3.6 - 135 : -135;
			var params = {table: schema};
			$http.post(location.href, params).success(function(dat){
				i++;
				if(dat != 'success') {
					$scope.fails.push(schema)
				}
				if (dat['message']) {
					util.message(dat['message']);
					return;
				}
				proc();
			}).error(function(){
				i++;
				$scope.fails.push(schema);
				proc();
			});
		}
		proc();
	})
	.controller('processor', function($scope, $http){
		var is_upgrade = "{php echo $is_upgrade}";
		if (packet && (packet.files.length > 0 || packet.files.schemas > 0)) {
			util.message('请先完成文件或是数据库同步', is_upgrade ? './index.php?c=module&a=manage-system&do=installed' : './index.php?c=cloud&a=upgrade');
			return false;
		}
		$scope.scripts = {php echo json_encode($scripts);};

		$scope.fails = [];
		var total = $scope.scripts.length;
		var i = 1;
		var error = function() {
			util.message('未能成功执行清理升级操作, 请联系开发者. ');
		}
		$scope.proc = function(showtips = 1) {
			var script = $scope.scripts.shift();
			if(!script) {
				if($scope.fails.length > 0) {
					error();
				} else {
					if(is_upgrade == 1) {
						{if $_GPC['batch']}
							location.href = '{url "system/module/upgrade" array("m" => $m, "flag" => 1, "batch_from_cloud" => 1)}';
						{else}
							location.href = '{url "system/module/upgrade" array("m" => $m, "flag" => 1)}';
						{/if}
						return;
						}

						var is_confirm = "{php echo $_GPC['is_confirm']}";
						if(is_confirm == 1) {
							if(confirm('已经成功执行升级操作! '+"\n"+' 由于数据库更新, 可能会产生多余的字段. 你可以按照需要删除')) {
								location.href = '{url "cloud/upgrade"}';
							} else {
								location.href = '{url "cloud/upgrade"}';
							}
						} else {
							if (showtips && packet.tips.length != 0) {
								$('#new-agreement').modal('show');
								return true;
							}
							util.message('已经成功执行升级操作! '+"\n"+' 由于数据库更新, 可能会产生多余的字段. 你可以按照需要删除.', '{url "cloud/upgrade"}');
						}
						return;
					}
				}
				$scope.script = script.fname;
				$scope.message = script.message;
				$scope.pragress = i + '/' + total;
				$scope.pragressPer = Math.floor((i / total) * 100);
				$scope.topPragress = $scope.pragressPer < 50 ? $scope.pragressPer * 3.6 - 135 : 45;
				$scope.bottomPragress = $scope.pragressPer > 50 ? ($scope.pragressPer - 50) * 3.6 - 135 : -135;
				var params = {fname: script.fname, tipversion: packet.tips.length != 0 ? packet.tips[0].version : ''};
				$http.post(location.href, params).success(function(dat){
					i++;
					if (dat == 'showtips') {
						$scope.proc(0);
						return;
					}
					if(dat != 'success') {
						$scope.fails.push(script.fname)
						error();
						return;
					}
					$scope.proc();
				}).error(function(){
					i++;
					$scope.fails.push(script.fname);
					error();
				});
			}
			$scope.agree = function(ifagree) {
				if (ifagree) {
					location.href = './index.php?c=cloud&a=process';
				} else {
					$scope.proc(0);
				}
			}
			$scope.proc();
		});
//-->
</script>
{if $step == 'files'}
{if !empty($_GPC['module_name'])}
<ul class="we7-step">
	<li class="active"><span class="content">1 安装应用</span></li>
	<li ><span class="content">2 分配应用权限</span> </li>
	<li><span class="content">3 安装成功</span> </li>
</ul>
{/if}
<div class="clearfix js-processor" ng-controller="FileProcessorCtrl" ng-cloak>
	<div class="upgrade-pragress we7-margin-bottom">
		<div class="wrapper top">
			<div class="circleProgress topcircle" ng-style="{transform: 'rotate('+ topPragress +'deg)'}"></div>
		</div>
		<div class="wrapper bottom">
			<div class="circleProgress bottomcircle" ng-style="{transform: 'rotate('+ bottomPragress +'deg)'}"></div>
		</div>
		<div class="circle">
			<img src="./resource/images/upgrade.png" alt="">
			<div class="pragress">
				{{pragressPer}}%
			</div>
		</div>
	</div>
	<div class="alert we7-page-alert">
		正在更新系统文件, 请不要关闭窗口
	</div>
	<div class="alert we7-page-alert">
		如果下载文件失败，可能造成的原因：写入失败，请仔细检查写入权限是否正确。
	</div>
	<div class="alert alert-info form-horizontal ng-cloak" >
		<dl class="dl-horizontal">
			<dt>整体进度</dt>
			<dd>{{pragress}}</dd>
			<dt>正在下载文件</dt>
			<dd class="text-over">{{file}}</dd>
		</dl>
		<dl class="dl-horizontal" ng-show="fails.length > 0">
			<dt>下载失败的文件</dt>
			<dd>
				<p class="text-danger text-over" ng-repeat="file in fails" style="margin:0;">{{file}}</p>
			</dd>
		</dl>
	</div>
	<script type="text/javascript">
		angular.module('cloudApp').value('config', {
			files : {php echo $packet['files'] ? json_encode($packet['files']) : '[]'},
			application_type: '{$application_type}',
			type : '{$type}',
			appname : '{$module_name}',
			is_upgrade : '{$is_upgrade}',
			account_type : "{$_GPC['support']}",
			tasknum : "{$packet['task']}",
		});
		angular.bootstrap($('.js-processor'), ['cloudApp']);
	</script>
</div>
{/if}
{if $step == 'schemas'}
<div class="clearfix js-processor" ng-cloak ng-controller="SchemasProcessorCtrl">
	{if empty($packet['schemas'])}
		<!-- 如果是安装模块,数据库操作完成后,不处理script,直接跳转到system/module/install -->
		<!-- 安装 -->
		{if !empty($packet['install'])}
			<script>
				location.href = '{url "module/manage-system/install" array("module_name" => $module_name, "flag" => 1, "support" => $_GPC["support"], "application_type" => $_GPC["application_type"])}';
			</script>
		{/if}
		<!-- 升级 -->
		<script>
			{if $_GPC['batch']}
				location.href = '{url "cloud/process" array("step" => "scripts", "module_name" => $module_name, "is_upgrade" => $is_upgrade, "batch" => 1, "support" => $_GPC["support"], "has_new_support" => $has_new_support, "application_type" => $_GPC["application_type"])}';
			{else}
				location.href = '{url "cloud/process" array("step" => "scripts", "module_name" => $module_name, "is_upgrade" => $is_upgrade, "support" => $_GPC["support"], "has_new_support" => $has_new_support, "application_type" => $_GPC["application_type"])}';
			{/if}
		</script>
	{/if}
	<div class="upgrade-pragress we7-margin-bottom">
		<div class="wrapper top">
			<div class="circleProgress topcircle" ng-style="{transform: 'rotate('+ topPragress +'deg)'}"></div>
		</div>
		<div class="wrapper bottom">
			<div class="circleProgress bottomcircle" ng-style="{transform: 'rotate('+ bottomPragress +'deg)'}"></div>
		</div>
		<div class="circle">
			<img src="./resource/images/upgrade.png" alt="">
			<div class="pragress">
				{{pragressPer}}%
			</div>
		</div>
	</div>
	<div class="alert alert-warning we7-page-alert">
		正在更新数据库, 请不要关闭窗口.
	</div>
	<div class="alert alert-info form-horizontal" >
		<dl class="dl-horizontal">
			<dt>整体进度</dt>
			<dd>{{pragress}}</dd>
			<dt>正在处理数据表</dt>
			<dd>{{schema}}</dd>
		</dl>
		<dl class="dl-horizontal" ng-show="fails.length > 0">
			<dt>处理失败的数据表</dt>
			<dd>
				<p class="text-danger" ng-repeat="schema in fails" style="margin:0;">{{schema}}</p>
			</dd>
		</dl>
	</div>
	<script type="text/javascript">
		angular.module('cloudApp').value('config', {
			schemas : {php echo $schemas ? json_encode($schemas) : '[]'},
			is_module_install : {php echo !empty($packet['install']) ? 'true' : 'false'},
		});
		angular.bootstrap($('.js-processor'), ['cloudApp']);
	</script>
</div>
{/if}
{if $step == 'scripts'}
<div class="clearfix js-processor" ng-cloak  ng-controller="processor">
	{if empty($packet['scripts']) || !empty($packet['type'])}
		<!-- 如果是更新模块,跳转到system/module/upgrade -->
		{if $is_upgrade == 1}
			<script>
				location.href = '{url "module/manage-system/upgrade" array("module_name" => $module_name, "flag" => 1, "support" => $_GPC["support"], "has_new_support" => $has_new_support)}';
			</script>
		{else}
			<script>
				var is_confirm = "{php echo $_GPC['is_confirm']}";
				if(is_confirm == 1) {
					if(confirm('已经成功执行升级操作! '+"\n"+' 由于数据库更新, 可能会产生多余的字段. 你可以按照需要删除')) {
						location.href = '{url "cloud/upgrade"}';
					} else {
						location.href = '{url "cloud/upgrade"}';
					}
				} else {
					require(['util'], function(u){
						u.message('已经成功执行升级操作! '+"\n"+' 由于数据库更新, 可能会产生多余的字段. 你可以按照需要删除.', '{url "cloud/upgrade"}');
					});
				}
			</script>
		{/if}
	{/if}
	<div class="upgrade-pragress we7-margin-bottom">
		<div class="wrapper top">
			<div class="circleProgress topcircle" ng-style="{transform: 'rotate('+ topPragress +'deg)'}"></div>
		</div>
		<div class="wrapper bottom">
			<div class="circleProgress bottomcircle" ng-style="{transform: 'rotate('+ bottomPragress +'deg)'}"></div>
		</div>
		<div class="circle">
			<img src="./resource/images/upgrade.png" alt="">
			<div class="pragress">
				{{pragressPer}}%
			</div>
		</div>
	</div>
	<div class="alert alert-warning we7-page-alert">
		正在数据迁移及清理操作, 请不要关闭窗口.
	</div>
	<div class="alert alert-info form-horizontal " >
		<dl class="dl-horizontal">
			<dt>整体进度</dt>
			<dd>{{pragress}}</dd>
			<dt>正在处理</dt>
			<dd>{{script}}<br />{{message}}</dd>
		</dl>
		<dl class="dl-horizontal" ng-show="fails.length > 0">
			<dt>处理失败的操作</dt>
			<dd>
				<p class="text-danger" ng-repeat="script in fails" style="margin:0;">{{script}}</p>
			</dd>
		</dl>
	</div>
	<div class="modal fade" id="new-agreement" tabindex="-1" role="dialog" aria-labelledby="rollback-label">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="text-center"><span>微擎新模式用户须知</span></h4>
				</div>
				<div class="modal-body overflow-auto">
					<div class="alert we7-page-alert">
						<i class="wi wi-info-sign"></i>本次更新，微擎系统模式改变，请认真阅读更新须知，谨慎选择。
					</div>
					<div class="we7-empty-blcok we7-margin-bottom-sm">
						{loop $packet['tips'] $tip}
						<p>{$tip['version']}更新微擎新模式详情：</p>
						<div>{$tip['message']}</div>
						{/loop}
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default we7-margin-right"  data-dismiss="modal" ng-click="agree(0)">稍后更新</button>
					<button type="button" class="btn btn-primary"  data-dismiss="modal" ng-click="agree(1)">同意并去更新</button>
				</div>
			</div>
		</div>
	</div>
	<script>
		angular.bootstrap($('.js-processor'), ['cloudApp']);
	</script>
</div>
{/if}
{template 'common/footer'}